﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;

using ECCIT;
using ECCIT.Data;
using ECCIT.Business.DAL;

using OgilvyOne.Model;
using OgilvyOne.UKNow.Globalization;
using OgilvyOne.UKNow.IDAL;
using OgilvyOne.UKNow.Model;

namespace OgilvyOne.UKNow.DAL
{
    public abstract class StaticGeneratableDAL : UKNowBaseDAL, IStaticGeneratableDAL
    {
        public StaticGeneratableDAL(int languageID) : base(languageID) { }
        public StaticGeneratableDAL(LanguageType languageType) : base(languageType) { }
        public StaticGeneratableDAL() : base() { }

        public virtual string COL_LANG_ID { get { return "COL_LANG_ID"; } }
        protected virtual string COL_IS_HTML_FILE { get { return "COL_IS_HTML_FILE"; } }
        protected virtual string COL_HTML_FILE_NAME { get { return "COL_HTML_FILE_NAME"; } }
        protected virtual string COL_SEO_TITLE { get { return "COL_SEO_TITLE"; } }
        protected virtual string COL_SEO_META_KEYWORDS { get { return "COL_SEO_META_KEYWORDS"; } }
        protected virtual string COL_SEO_META_DESC { get { return "COL_SEO_META_DESC"; } }

        public override Type ModelType { get { return typeof(StaticGeneratableModel); } }
        public abstract IField PrimaryKey { get; }
        [PrimaryKey]
        public virtual IField LanguageID { get { return FieldFactory.Create(ModelType, "LanguageID", ColumnFactory.CreateColumn(COL_LANG_ID, DataType.Int)); } }
        public virtual IField IsHtmlFile { get { return FieldFactory.Create(ModelType, "IsHtmlFile", ColumnFactory.CreateColumn(COL_IS_HTML_FILE, DataType.Bit)); } }
        public virtual IField HtmlFileName { get { return FieldFactory.Create(ModelType, "HtmlFileName", ColumnFactory.CreateColumn(COL_HTML_FILE_NAME, DataType.String, 100)); } }
        public virtual IField SEOTitle { get { return FieldFactory.Create(ModelType, "SEOTitle", ColumnFactory.CreateColumn(COL_SEO_TITLE, DataType.String, 255)); } }
        public virtual IField SEOMetaKeywords { get { return FieldFactory.Create(ModelType, "SEOMetaKeywords", ColumnFactory.CreateColumn(COL_SEO_META_KEYWORDS, DataType.String, 500)); } }
        public virtual IField SEOMetaDescription { get { return FieldFactory.Create(ModelType, "SEOMetaDescription", ColumnFactory.CreateColumn(COL_SEO_META_DESC, DataType.String, 500)); } }

        public virtual ResultInfo SaveSEOInfo(StaticGeneratableModel model)
        {
            ResultInfo result = new ResultInfo(false);
            IField[] updateFields = new IField[] { IsHtmlFile, HtmlFileName, SEOTitle, SEOMetaKeywords, SEOMetaDescription };
            string whereClause = string.Format(" where {0}={1} and {2}={3}", LanguageID.Column.Name, (int)this.Language.LanguageType, PrimaryKey.Column.Name, model.ID.Value);
            string cmd = DBHelper.GetUpdateCommandText(this.TableName, updateFields, whereClause);
            IDataParameter[] parms = DBHelper.GetDataParameters(updateFields);
            parms[0].Value = model.IsHtmlFile;
            parms[1].Value = model.HtmlFileName;
            parms[2].Value = model.SEOTitle;
            parms[3].Value = model.SEOMetaKeywords;
            parms[4].Value = model.SEOMetaDescription;

            try
            {
                int rows = DBHelper.ExecuteNonQuery(cmd, parms);
                if (rows > 0)
                    result.Result = true;
            }
            catch (Exception ex)
            {
                result.Message = "Error occurred in StaticGeneratableDAL.SaveSEOInfo. Message: " + ex.Message;
            }
            return result;
        }

        public virtual ResultInfo GetSEOInfo(int id)
        {
            ResultInfo result = new ResultInfo(false);
            string whereClause = string.Format(" where {0}={1} and {2}={3}", LanguageID.Column.Name, (int)this.Language.LanguageType, PrimaryKey.Column.Name, id);
            string cmd = DBHelper.GetSelectCommandText(this.TableName,
                new IField[] { IsHtmlFile, HtmlFileName, SEOTitle, SEOMetaKeywords, SEOMetaDescription },
                whereClause);

            try
            {
                StaticGeneratableModel model = null;
                using (IDataReader reader = DBHelper.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        model = new StaticGeneratableModel();
                        model.IsHtmlFile = (reader[IsHtmlFile.Column.Name] == DBNull.Value) ? true : (bool)reader[IsHtmlFile.Column.Name];
                        model.HtmlFileName = (reader[HtmlFileName.Column.Name] == DBNull.Value) ? string.Empty : (string)reader[HtmlFileName.Column.Name];
                        model.SEOTitle = (reader[SEOTitle.Column.Name] == DBNull.Value) ? string.Empty : (string)reader[SEOTitle.Column.Name];
                        model.SEOMetaKeywords = (reader[SEOMetaKeywords.Column.Name] == DBNull.Value) ? string.Empty : (string)reader[SEOMetaKeywords.Column.Name];
                        model.SEOMetaDescription = (reader[SEOMetaDescription.Column.Name] == DBNull.Value) ? string.Empty : (string)reader[SEOMetaDescription.Column.Name];
                    }
                }

                result.Result = true;
                result.ReturnObject = model;
            }
            catch (Exception ex)
            {
                result.Message = "Error occurred in StaticGeneratableDAL.GetSEOInfo. Message: " + ex.Message;
            }

            return result;
        }
    }
}
